class IUP_SCROLL_BOX
-- Creates a native container that allows its child to be scrolled.
--
-- The box allows the application to create a virtual space for the dialog that 
-- is actually larger than the visible area. The current size of the box 
-- defines the visible area. The natural size of the child (and its children) 
-- defines the virtual space size. So the IupScrollBox does not depend on its 
-- child size or expansion, and its natural size is always 0x0.
--
-- The user can move the box contents by dragging the background. Also the 
-- mouse wheel scrolls the contents vertically.
--
-- The box can be created with no elements and be dynamic filled using 
-- append or insert.

inherit
	IUP_CANVAS
	IUP_WIDGET_CLIENTSIZE
	IUP_WIDGET_CLIENTOFFSET

create {ANY}
	scroll_box

feature {ANY}

	scroll_box(child: IUP_WIDGET)
		-- A new scroll box. child is an interface element which will receive the 
		-- box. It can be Void.
		local
			a_scroll_box: POINTER
		do
			a_scroll_box := int_scroll_box(child.widget)
			set_widget(a_scroll_box)
		end

	-- Attributes

	set_canvas_box (state: BOOLEAN)
		-- (non inheritable): enable the behavior of a canvas box instead of a 
		-- regular container. Default: "False".
		do
			iup_open.set_attribute(Current, "CANVASBOX", boolean_to_yesno(state))
		end

	set_childoffset (horizontal, vertical: INTEGER)
		require
			horizontal >= 0
			vertical >= 0
		local
			offset: STRING
		do
			offset := horizontal.to_string
			offset.append_string("x")
			offset.append_string(vertical.to_string)

			iup_open.set_attribute(Current, "CHILDOFFSET", offset)
		end

	get_childoffset: TUPLE[INTEGER, INTEGER]
		-- Return the offset of the child.
		local
			offset: STRING
		do
			offset := iup_open.get_attribute(Current, "CHILDOFFSET")
			Result := components_of_size(offset)
		end

	set_layout_drag (state: BOOLEAN)
		-- (non inheritable) [Windows Only]: When the scrollbar is moved 
		-- automatically update the children layout. Default: "True". If set to 
		-- "False" then the layout will be updated only when the mouse drag
		-- is released.
		do
			iup_open.set_attribute(Current, "LAYOUTDRAG", boolean_to_yesno(state))
		end

	-- Moves
	
	scroll_to_top
		do
			iup_open.set_attribute(Current, "SCROLLTO", "TOP")
		end

	scroll_to_bottom
		do
			iup_open.set_attribute(Current, "SCROLLTO", "BOTTOM")
		end

	scroll_to (x, y: INTEGER)
		-- Position the scroll at the given x,y coordinates relative to the box 
		-- top-left	corner.
		local
			position: STRING
		do
			position := x.to_string
			position.append_string(",")
			position.append_string(y.to_string)

			iup_open.set_attribute(Current, "SCROLLTO", position)
		end

	scroll_to_child (child_name: STRING)
		-- (write-only): position the scroll at the top-left corner of the given 
		-- child located by its name. Use set_attribute_handle to associate a 
		-- name. The child must be contained in the Scrollbox hierarchy.
		do
			iup_open.set_attribute(Current, "SCROLLTOCHILD", child_name)
		end

	scroll_to_child_widget (child_widget: IUP_WIDGET)
		-- (write-only): same as scroll_to_child but directly using the child
		-- widget.
		do
			iup_open.set_attribute_widget(Current, "SCROLLTOCHILD_HANDLE", child_widget)
		end

feature {}

	-- Internal
	
	int_scroll_box (wgt: POINTER): POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupScrollBox"
         }"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
